
// get form input elements
axios.defaults.baseURL="http://ajax-api.itheima.net"
const usernameInput = document.querySelector('[name="username"]');
const passwordInput = document.querySelector('[name="password"]');
const form=document.querySelector('form')
// add event listener to login button
const btn=document.querySelector('.btn')
btn.addEventListener("click", async () =>{
event.preventDefault();
const username = usernameInput.value;
const password = passwordInput.value;
const data=serialize(form, {hash:true})
if (!username || !password) {
return tip("请输入用户名和密码")             
}
try {
const res = await axios({
url:'/login',
method: "POST",
data
});
console.log(res);
tip(res.data.message);
 localStorage.setItem('username',res.data.data.username)
 const name=localStorage.getItem('username')
 console.log(name);
//       setTimeout(()=>{
//   location.href='./index.html'
// },500)

} catch (error) {
tip(error.response.data.message);
}
});

